package cn.edu.fzu.homemaking.helper;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.iai.v20180301.IaiClient;
import com.tencentcloudapi.iai.v20180301.models.CompareFaceRequest;
import com.tencentcloudapi.iai.v20180301.models.CompareFaceResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class FaceVerifyHelper {

    private static final Logger log = LoggerFactory.getLogger(FaceVerifyHelper.class);

    @Value("${tencent.cloud.secretId}")
    private String              secretId;
    @Value("${tencent.cloud.secretKey}")
    private String              secretKey;

    public boolean isSameFace(String faceA, String faceBUrl) {
        try {
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(new HttpProfile());
            IaiClient client = new IaiClient(new Credential(secretId, secretKey), null, clientProfile);
            CompareFaceRequest req = new CompareFaceRequest();
            req.setImageA(faceA);
            req.setUrlB(faceBUrl);
            CompareFaceResponse resp = client.CompareFace(req);
            //分数超过或等于80则认为识别成功
            return resp.getScore() >= 95;
        } catch (TencentCloudSDKException e) {
            log.error("tencent face error", e);
            return false;
        }
    }

}
